package com.kimstore.dao.impl;

import com.kimstore.bean.Customer;
import com.kimstore.bean.Item;
import com.kimstore.bean.Order;
import com.kimstore.bean.Staff;
import com.kimstore.dao.OrderDao;
import com.kimstore.utilities.QueryElements;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import javax.persistence.Query;

public class OrderDaoImpl extends GenericJpaDaoImpl<Order, Integer> implements OrderDao {

    @Override
    public List<Order> findByCustomer(Customer customer) {
        Query query = getEntityManager().createQuery("from Order order where order.customer  = :customer order by order.orderDate");
        query.setParameter("customer", customer);

        List<Order> orders = query.getResultList();

        return orders;
    }

    @Override
    public List<Order> findByStaff(Staff staff) {
        Query query = getEntityManager().createQuery("from Order order where order.staff  = :staff order by order.orderDate");
        query.setParameter("staff", staff);

        List<Order> orders = query.getResultList();

        return orders;
    }
    
    @Override
    public List<Order> findByStaff(Staff staff, Date startDate, Date endDate) {
        Query query = getEntityManager().createQuery("from Order order where order.staff  = :staff and order.orderDate between :startDate and :endDateorder by order.orderDate");
        query.setParameter("staff", staff);
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);

        List<Order> orders = query.getResultList();

        return orders;    }

    @Override
    public List<Order> findAll(int startIndex, int maxNumber) {
        Query query = getEntityManager().createQuery("from Order order order by order.orderDate");
        query.setFirstResult(startIndex);
        query.setMaxResults(maxNumber);

        List<Order> orders = query.getResultList();

        return orders;
    }

    public long count() {
        Query query = getEntityManager().createQuery("select count(*) from Order order");
        long count = (long) query.getSingleResult();

        return count;
    }   

    

}
